package com.lyf.controller.admin;

import com.lyf.dto.SetmealDTO;
import com.lyf.dto.SetmealPageQueryDTO;
import com.lyf.pojo.Setmeal;
import com.lyf.result.PageResult;
import com.lyf.result.Result;
import com.lyf.service.SetmealService;
import com.lyf.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "套餐管理")
@Slf4j
@RequestMapping("/admin/setmeal")
@RestController
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @ApiOperation("新增套餐")
    @PostMapping
    @CachePut(cacheNames = "setmealCache", key = "#setmealDTO.categoryId")
    public Result save(@RequestBody SetmealDTO setmealDTO){
        log.info("新增套餐：{}",setmealDTO);
        setmealService.save(setmealDTO);
        return Result.success();
    }

    @ApiOperation("删除套餐")
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result delete(@RequestParam List<Long> ids){
        log.info("批量删除：{}",ids);
        setmealService.deleteBatch(ids);
        return Result.success();
    }

    @ApiOperation("修改套餐")
    @PutMapping
    @CacheEvict(cacheNames = "setmealCache", key = "#setmealDTO.categoryId")
    public Result update(@RequestBody SetmealDTO setmealDTO){
        log.info("修改套餐：{}",setmealDTO);
        setmealService.update(setmealDTO);
        return Result.success();
    }

    @ApiOperation("套餐分页查询")
    @GetMapping("/page")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("分页查询：{}",setmealPageQueryDTO);
        PageResult results = setmealService.pageQuery(setmealPageQueryDTO);
        return Result.success(results);
    }

    @ApiOperation("查询套餐详情")
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id){
        log.info("查询套餐详情：{}",id);
        SetmealVO setmealVO = setmealService.getByIdWithDishes(id);
        return Result.success(setmealVO);
    }

    @ApiOperation("套餐起售停售")
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCache", key = "#id")
    public Result startOrStop(@PathVariable Integer status, Long id){
        log.info("套餐起售停售：{}",id);
        Setmeal setmeal = Setmeal.builder().id(id).status(status).build();
        setmealService.startOrStop(setmeal);
        return Result.success();
    }

}
